using System;
using System.Drawing;
using System.Windows.Forms;
using OhioState.Controls.AssetMgr;

namespace OhioState.Controls
{
    /// <summary>
    /// This is a very simple color-selection control; it has 
    /// four sliders to select rgba components, and a preview panel
    /// to see the result.
    /// There are also a button for creating new two-d transfer
    /// function and am asset manager drop down control to select
    /// a volume which histogram we'd like to work with.
    /// These last two components should be removed - they don't
    /// make sense if someone would like to use only color
    /// selection regardless of two-d transfer functions.
    /// </summary>
    public partial class ColorChooserCtrl : UserControl
    {
        private TwoDTransferFunctionCtrl transferFunction;
        private int red;
        private int green;
        private int blue;
        private int alpha;

        public TwoDTransferFunctionCtrl TransferFunction
        {
            get { return transferFunction; }
            set { transferFunction = value; }
        }
        
        public AssetManagerDropDownControl AssetManagerCtrl
        {
            get { return assetManagerDropDownControl1; }
        }

        public CheckBox EqualizeGradientsHistogramCheckBox
        {
            get { return eqHistogramCheckBox; }
        }

        public ColorChooserCtrl()
        {
            InitializeComponent();
        }
        
        public ColorChooserCtrl(TwoDTransferFunctionCtrl transferFunction)
        {
            this.transferFunction = transferFunction;
            InitializeComponent();
        }

        public void SetColor(int r, int g, int b, int a)
        {
            this.red = r;
            this.green = g;
            this.blue = b;
            this.alpha = a;
            trackBar1.Value = r;
            trackBar2.Value = g;
            trackBar3.Value = b;
            trackBar4.Value = a;
            UpdatePanelColor();
        }

        private void trackBar1_valueChangedHandler(object sender, EventArgs e)
        {
            red = ((System.Windows.Forms.TrackBar)sender).Value;
            UpdatePanelColor();
        }

        private void trackBar2_valueChangedHandler(object sender, EventArgs e)
        {
            green = ((System.Windows.Forms.TrackBar)sender).Value;
            UpdatePanelColor();
        }

        private void trackBar3_valueChangedHandler(object sender, EventArgs e)
        {
            blue = ((System.Windows.Forms.TrackBar)sender).Value;
            UpdatePanelColor();
        }

        private void trackBar4_valueChangedHandler(object sender, EventArgs e)
        {
            alpha = ((System.Windows.Forms.TrackBar)sender).Value;
            UpdatePanelColor();
        }

        private void UpdatePanelColor()
        {
            panel1.BackColor = Color.FromArgb(alpha, red, green, blue);
            transferFunction.MarkerColorChanged(panel1.BackColor);
            //panel1.Update();//Update still causes flickering - as well as Invalidate()
        }

        private void createTFButton_Click(object sender, EventArgs e)
        {
            transferFunction.CreateTransferFunction();
        }
    }
}
